package com.fypg.module.medical.dal.mysql.doctorapplication;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fypg.framework.mybatis.core.mapper.BaseMapperX;
import com.fypg.module.medical.controller.app.doctorapplication.vo.AppDoctorApplicationReqVO;
import com.fypg.module.medical.dal.dataobject.doctorapplication.DoctorApplicationDO;
import com.fypg.module.medical.dal.dataobject.doctorapplicationhealthcode.DoctorApplicationHealthCodeDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 医生应用列表 Mapper
 *
 * @author PJY
 */
@Mapper
public interface DoctorApplicationMapper extends BaseMapperX<DoctorApplicationDO> {

    /**
     * 根据健康码和客户端类型，联表查询有权限的应用程序列表
     * 相比于之前 C# 代码的先查所有（可筛选 ClientType）应用再遍历应用对应电子健康码，
     * 这里做了联表查询避免在循环中查询数据库
     *
     * @param reqVO 请求参数
     * @return 应用程序列表
     */
    default List<DoctorApplicationDO> selectListByHealthCode(AppDoctorApplicationReqVO reqVO) {
        MPJLambdaWrapper<DoctorApplicationDO> wrapper = new MPJLambdaWrapper<DoctorApplicationDO>()
            .selectAll(DoctorApplicationDO.class)
            .innerJoin(DoctorApplicationHealthCodeDO.class, DoctorApplicationHealthCodeDO::getApplicationId, DoctorApplicationDO::getApplicationId)
            .eq(DoctorApplicationDO::getDeleted, 0)
            .eq(DoctorApplicationDO::getEnabledMark, 1)
            .eq(DoctorApplicationHealthCodeDO::getDeleted, 0)
            .eq(DoctorApplicationHealthCodeDO::getEnabledMark, 1)
            .eq(DoctorApplicationHealthCodeDO::getHealthCode, reqVO.getHealthCode())
            .eq(StringUtils.isNotBlank(reqVO.getClientType()), DoctorApplicationDO::getClientType, reqVO.getClientType())
            .orderByAsc(DoctorApplicationDO::getSortCode);

        return this.selectJoinList(DoctorApplicationDO.class, wrapper);
    }

}
